package com.demo.action;

import com.demo.domain.Customer;
import com.demo.domain.Shangping;
import com.demo.service.ShangPingService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import java.util.List;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2020/4/19
 * @Content:
 */
public class ShangPingAction extends ActionSupport implements ModelDriven<Shangping> {

    private Shangping shangping = new Shangping();

    private ShangPingService shangPingService;

    public void setShangPingService(ShangPingService shangPingService) {
        this.shangPingService = shangPingService;
    }

    //添加
    public String add() throws Exception {

        shangPingService.saveShangping(shangping);
        return "ShangPing_saveSuccess";

    }

    // 查找商品信息列表
    public String list() throws Exception {
        List<Shangping> list = shangPingService.find();
        ActionContext.getContext().getValueStack().set("list", list);
        return "ShangPing_findSuccess";
    }

    // 删除的方法
    public String delete() {
        // 使用模型驱动获取表单提交cid值
        int s_id = shangping.getS_id();
        // 删除规范操作：先查询，再删除（级联表会有影响）
        Shangping s = shangPingService.findOne(s_id);
        // 调用方法删除
        if (s != null) {
            shangPingService.delete(s);
        }
        return "delete";
    }

    // 通过id修改 跳转到修改页面
    public String showShangPing() throws Exception {
        Integer cid = shangping.getS_id();
        Shangping s = shangPingService.findOne(cid);
        ServletActionContext.getRequest().setAttribute("shangping", s);
        return "showShangPing";
    }

    //修改用户
    public String update() {
        shangPingService.update(shangping);
        return "ShangPing_updateSuccess";
    }

    // 到查询商品页面
    public String toSelectShangPingPage() {

        return "toSelectShangPingPage";
    }

    // 多条件查询
    public String moreCondition() throws Exception {

        List<Shangping> list = shangPingService.findMoreCondition(shangping);

        ActionContext.getContext().put("list", list);
        return "moreCondition";
    }


    @Override
    public Shangping getModel() {
        return shangping;
    }
}
